package com.java.demo.component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

// 注意是实现接口
// 此处没有加五大类注解，用的是 spring-config.xml 中的bean标签
public class BeanCompoent implements BeanNameAware, BeanPostProcessor {


    public void sayHi() {
        System.out.println("执行 sayHi()");
    }

    @Override
    public void setBeanName(String s) {
        // 1.执行通知
        System.out.println("执行了通知" + s);
    }

    /**
     * xml 方式的初始化方法
     */
    public void myInit() {
        System.out.println("XML 方式初始化");
    }

    /**
     * 注解 方式的初始化方法  PostConstruct
     */
    @PostConstruct
    public void doPostConstruct() {
        System.out.println("注解初始化方法");
    }

    @PreDestroy
    public void doPreDestroy() {
        System.out.println("销毁方法");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        //return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
        System.out.println("初始化前置方法");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
        System.out.println("初始化后置方法");
        return bean;
    }
}
